﻿using Ledict.ConditionExtract.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Frm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private TreeNode clicknode;//点击节点树后，保存该节点
        CaseCauseBLL caseCauseBLL = new CaseCauseBLL();
        List<string> str = new List<string>();
        string other = string.Empty;
        private void button1_Click(object sender, EventArgs e)
        {
            string[] res = caseCauseBLL.GetElementCcid(this.textBox1.Text);
            if (res.Length == 0)
            {
                MessageBox.Show("无此案由");
                return;
            }
            else//加载树
            {
                treeView1.Nodes.Clear();
                var node = new TreeNode();
                node.Text = res[1];
                node.Tag = this.textBox1.Text.Trim();
                node.Nodes.Add(new TreeNode());
                treeView1.Nodes.Add(node);

                other = label1.Text = res[0];
                str = res[2].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList<String>();
                node.Expand();
            }

        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            CaseCauseTreeChild(e.Node);
        }

        readonly TreeDataBLL trbll = new TreeDataBLL();
        /// <summary>
        /// 下级树
        /// </summary>
        /// <param name="parentNode"></param>
        private void CaseCauseTreeChild(TreeNode parentNode)
        {
            var pid = parentNode.Tag.ToString();// 父级ID
            DataTable caseTreeNodeRode;
            List<string> fullPath = parentNode.FullPath.Split('\\').ToList<String>();
            if (str.Contains(pid) || fullPath.Contains("事实要素") || fullPath.Contains("诉请要素") || fullPath.Contains("基本要素"))
            {
                caseTreeNodeRode = trbll.NodeTree_Tree(pid);//父级ID的下级节点
            }
            else
            {
                caseTreeNodeRode = caseCauseBLL.GetNodeTree(pid, other);//父级ID的下级节点
            }

            if (caseTreeNodeRode.Rows.Count == 0)
                return;
            for (var i = 0; i < caseTreeNodeRode.Rows.Count; i++)
            {
                var node = new TreeNode();
                node.Text = caseTreeNodeRode.Rows[i]["name"].ToString();
                node.Tag = caseTreeNodeRode.Rows[i]["id"];
                if (trbll.NodeTree_Tree(node.Tag.ToString()).Rows.Count > 0)//节点有无下级
                {
                    node.Nodes.Add(new TreeNode());
                }
                parentNode.Nodes.Add(node);//父级节点添加节点               
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                clicknode = e.Node;//保存节点
                label3.Text = clicknode.Tag.ToString();
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}
